// pages/product/product.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sortByAll: true,
    sortBySale: false,
    sortByPrice: false,
    productDetails: [],
    limit: [],
    skip: 0,
    num: 0,
    priceUp: false,
    byPrice:false,
    value:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getProduct(num) {
    const _ = wx.cloud.database().command
    let db = wx.cloud.database().collection('product_details')
      .where(
        _.or([{
          name:wx.cloud.database().RegExp({
            regexp:this.data.value,
            options:'i'
          })
        },
        {
          synopsis:wx.cloud.database().RegExp({
            regexp:this.data.value,
            options:'i'
          })
        }])
      )
    if (this.data.sortBySale) {
      db = db.orderBy('sale', 'desc')
    } else if (this.data.sortByPrice && this.data.priceUp) {
      db = db.orderBy('price_first', 'esc')
    } else if (this.data.sortByPrice && !this.data.priceUp) {
      db = db.orderBy('price_first', 'desc')
    }
    db.skip(this.data.skip).limit(10)
      .get()
      .then(res => {
        console.log('前skip:', this.data.skip)
        if (num == 0) {
          this.setData({
            productDetails: res.data,
            limit: res.data,
            skip: res.data.length
          })
        } else {
          this.setData({
            productDetails: this.data.productDetails.concat(res.data),
            limit: res.data,
            skip: this.data.productDetails.concat(res.data).length
          })
        }
        wx.hideLoading({
          success: (res) => {},
        })
        console.log('后skip:', this.data.skip)
        console.log('length:', this.data.productDetails.length)
        console.log('请求成功', res)
      })
      .catch(err => {
        wx.hideLoading({
          success: (res) => {},
        })
        console.log('请求失败', err)
      })
  },
  onLoad: function (options) {
    this.sortByAll()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.limit.length < 10) {
      wx.showToast({
        title: '没有更多商品了',
        icon: 'none'
      })

    } else {
      wx.showLoading({
        title: '加载中'
      })
      this.getProduct(this.data.num)
    }

  },

  sortByAll() {
    this.setData({
      sortByAll: true,
      sortBySale: false,
      sortByPrice: false,
      skip: 0,
      num: 0,
      byPrice:false,
      priceUp: false
    })
    this.getProduct(this.data.num)
    this.setData({
      num: 1
    })
  },
  sortBySale() {
    this.setData({
      sortByAll: false,
      sortBySale: true,
      sortByPrice: false,
      skip: 0,
      num: 0,
      byPrice:false,
      priceUp: false
    })
    this.getProduct(this.data.num)
    this.setData({
      num: 1
    })
  },
  sortByPrice() {
    this.setData({
      sortByAll: false,
      sortBySale: false,
      sortByPrice: true,
      skip: 0,
      num: 0,
      byPrice:true,
      priceUp: !this.data.priceUp,
    })
    this.getProduct('', this.data.num)
    this.setData({
      num: 1
    })
  },
  onSearch(e) {
    this.setData({
      value:e.detail
    })
    this.sortByAll()
  },
  onCancel(){
    this.setData({
      value:""
    })
    this.sortByAll()
  },
  //跳转到详情页面
  goToDetail(e) {
    wx.navigateTo({
      url: '../productDetail/productDetail?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})